define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dijit/Tree'
   ],
   function(declare, lang, Tree)
   {
      return declare([Tree],
      {
         constructor:function(args)
         {
            dojo.safeMixin(this, args);
         },
         postscript: function(args)
         {
            this.inherited(arguments);
         },
         selectTreeNodeById: function(lookForId)
         {
            console.log("See model root=", this.model.root);
            var buildme = new Array();
            var result = this.recursiveHunt(lookForId, this.model, buildme, this.model.root);
            console.log("*** FINISHED: result ", result, " buildme ", buildme);
            console.dir(result);
            if (result && result.length > 0)
               this.attr('path', result);
         },
         recursiveHunt: function(lookForId, buildme, pItem)
         {
            console.log(">> recursiveHunt, pItem ", pItem, " looking for ", lookForId);
            var id = this.model.getIdentity(pItem);
            buildme.push(id);
            if (id == lookForId) {
               // Return the buildme array, indicating a match was found
               console.log("++ FOUND pItem ", pItem, " buildme now = ", buildme);
               return buildme;
            }
            for (var idx in pItem.children) {
               // start a new branch of buildme, starting with what we have so far
               var buildmebranch = buildme.slice(0);
               console.log("Branching into ", this.model.store.getValue(pItem.children[idx], 'name'), ", buildmebranch=", buildmebranch);
               var r = recursiveHunt(lookForId, this.model, buildmebranch, pItem.children[idx]);
               // If a match was found in that recurse, return it.
               //  This unwinds the recursion on completion.
               if (r)
                  return r;
            }
            // Return undefined, indicating no match was found
            return undefined;
         },
         _destroy: function(){
            if(this._curSearch){
               clearTimeout(this._curSearch.timer);
               delete this._curSearch;
            }
            if(this.rootNode){
               this.rootNode.destroyRecursive();
            }
            if(this.dndController && !dojo.isString(this.dndController)){
               this.dndController.destroy();
            }
            this.rootNode = null;
         },
         refresh: function(){
            /* remember current paths:
             * simplify all nodes of paths-array to strings of identifiers because
             * after reload the nodes may have different ids and therefore the
             * paths may not be applied */
            this._reloadPaths = dojo.map(this.get('paths'), dojo.hitch(this, function(path) {
               return dojo.map(path, dojo.hitch(this, function(pathItem) {
                  return this.model.getIdentity(pathItem) + '';
               }));
            }));
      
            // This seems to help with the exponential slowing of subsequest refreshes.
            var children = this.rootNode.getChildren();
            for(var i = 0; i < children.length; i++)
               children[i].destroyRecursive();
      
            /* reset tree: */
            this._destroy();
            this.dndController = "dijit.tree._dndSelector";
      
            /* unset the store's cache (if existing).
             * TODO: currently only tested on JsonRestStore! */
            if (dojox && dojox.rpc && dojox.rpc.Rest && dojox.rpc.Rest._index) {
               for (idx in dojox.rpc.Rest._index) {
                  if (idx.match("^" + this.model.store.target)) {
                     delete dojox.rpc.Rest._index[idx];
                  }
               }
            }
      
            // reset the tree.model is root
            this.model.constructor({
               rootId: '0',
               rootLabel: ''
            });
      
            // rebuild the tree
            this.postMixInProperties();
            this.postCreate();
      
         }
      });
   }
);
